package com.longder.kindergarten.controller;

import com.longder.kindergarten.entity.dto.Response;
import com.longder.kindergarten.entity.po.Child;
import com.longder.kindergarten.service.ChildrenManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 娃娃管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/children")
public class ChildrenManageController {

    @Resource
    private ChildrenManageService childrenManageService;

    /**
     * 添加或编辑学生
     */
    @PostMapping("/addOrEditChild")
    public Response addChild(@RequestBody Child child){
        try {
            childrenManageService.addOrUpdateChild(child);
            return Response.success("保存成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Response.error("保存失败："+e.getMessage());
        }
    }

    /**
     * 查询某班级下所有学生
     * @param classId 班级id
     */
    @GetMapping("/listChildren/{classId}")
    public Response listChildrenByClass(@PathVariable("classId")Long classId){
        return Response.success(childrenManageService.listByClassId(classId));
    }
}
